const xss = require('xss')
const { exec } = require('../db/mysql')

/**
 *  拿回复  根据帖子id
 * @param {贴id} tid 
 */
const getReply = async (tid) => {
    const sql = `
        SELECT	
            u1.UName,
            u1.UHead,
            u1.UID,
            rr1.RContent,
            rr1.Rtime
        FROM
            bbsuser u1,
            (SELECT*FROM bbsreply r1 WHERE  r1.RTID=${tid}) rr1
        WHERE 
            rr1.RUID=U1.UID
        order by rr1.Rtime desc
    `
    return await exec(sql)

}

/**
 * 回复帖子
 * @param {回复主贴id} rtid 
 * @param {所在板块} rsid 
 * @param {回复人} ruid 
 * @param {回复内容} rcontent 
 * @param {回复时间} rtime 
 */
const replyBlog = async (rtid, rsid, ruid, rcontent, rtime,rCount) => {

    const sql = `
        insert into bbsreply (rtid, rsid, ruid, rcontent,rtime)
        values (${rtid}, ${rsid}, ${ruid}, '${rcontent}',${rtime});
    `
    const sql2=`
        UPDATE  
            bbstopic
        SET 
            TReplyCount=${rCount},
            TLastReplyUselD=${ruid}
        WHERE
            tid=${rtid}
    `
    const insertData = await exec(sql)
    exec(sql2)

    return {
        id: insertData.insertId
    }

}

/**
 * 发帖
 * @param {所在板块} tsid 
 * @param {用户id} tuid 
 * @param {帖子主题} ttopic 
 * @param {帖子内容} tcontent 
 * @param {创建时间} ttime 
 */
const sendNewBlog = async (tsid, tuid, ttopic, tcontent, ttime) => {

    const sql = `
        insert into bbstopic (tsid, tuid, ttopic, tcontent,ttime,TLastReplayTime)
        values ('${tsid}', '${tuid}', '${ttopic}', '${tcontent}','${ttime}','${ttime}');
    `
    const insertData = await exec(sql)
    return {
        id: insertData.insertId
    }

}


/**
 * 获得全部帖子
 * @param {指定用户} uid 
 * @param {关键字} keyword 
 * @param {板块} classes
 */
const getAllBlog = async (uid, keyword, tsid) => {
    let sql = `
        SELECT
            u1.UID,t1.tid,u1.UHead,t1.TReplyCount,t1.TClickCoount,t1.tsid,t1.TTopic,
            h1.Uhead,t1.TLastReplayTime,t1.Ttop
        FROM
            bbsuser u1,
            bbstopic t1,
            (SELECT Uhead,u1.UID,t1.TID FROM bbsuser u1,bbstopic t1 WHERE u1.UID=t1.TLastReplyUselD) h1
        WHERE 
            u1.UID=t1.TUID  AND  h1.tid=t1.TID

        `
    if (uid) {
        sql += ` and t1.tuid='${uid}' `
    }
    if (keyword) {
        sql += ` and t1.ttopic like '%${keyword}%' `
    }
    if (tsid) {
        sql += ` and t1.tsid='${tsid}' `
    }

    sql += `  order by t1.TTime  desc  `

    return await exec(sql)
}

// 点击帖子标题查看帖子  tid=帖子id
const getDetail = async (tid, clickCount) => {
    let sql = `SELECT
                t1.TTopic,
                t1.TTime,
                u1.UName,
                t1.TSID,
                t1.TClickCoount,
                t1.TContent,
                t1.Tgood,
                u1.uhead,
                t1.TReplyCount
            FROM
                bbsuser u1,
                bbstopic t1
            WHERE 
            u1.UID=t1.TUID AND t1.tid=${tid}
        `


    const rows = await exec(sql)
    let count = parseInt(clickCount) + 1
    let sql2 = ` UPDATE bbstopic  SET TClickCoount=${count} WHERE TID=${tid} `
    exec(sql2)
    return rows[0]
}

/**
 * 
 * @param {编辑的帖子对象} body 
 */
const updateBlog = async (body = {}) => {
    // id 就是要更新博客的 id
    // blogData 是一个博客对象，包含 title content 属性
    const tuid = xss(body.tuid)
    const tid = xss(body.tid)
    const ttopic = xss(body.ttopic)
    const tcontent = xss(body.tcontent)

    const sql = `
        update bbstopic set ttopic='${ttopic}', tcontent='${tcontent}' where tid=${tid} and tuid=${tuid}
    `

    const updateData = await exec(sql)
    if (updateData.affectedRows > 0) {
        return true
    }
    return false
}

const delBlog = async (id, author) => {
    // id 就是要删除博客的 id
    const sql = `delete from blogs where id='${id}' and author='${author}';`
    const delData = await exec(sql)
    if (delData.affectedRows > 0) {
        return true
    }
    return false
}

module.exports = {
    getAllBlog,
    getDetail,
    updateBlog,
    delBlog,
    sendNewBlog,
    replyBlog,
    getReply
}